//
//  SNRetunGoodsDataManager.m
//  app
//
//  Created by 王少宁 on 15/5/21.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNReturnGoodsDataManager.h"

@interface SNReturnGoodsDataManager ()
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@property (nonatomic, retain) JKHttpClient * http;
@end

@implementation SNReturnGoodsDataManager

+ (SNReturnGoodsDataManager *)shareManager
{
    AS_SINGLETON(SNReturnGoodsDataManager, manager, {
        manager = [[SNReturnGoodsDataManager alloc] init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.http = [[JKHttpClient alloc] init];
    })
    return manager;
}

- (void)returnGoodsTextWithResult:(SNReturnGoodsUploadTextHttpResult *)result sn:(NSString *)sn returnsfor:(NSString *)returnsfor productIDs:(NSString *)productIDs contacts:(NSString *)contacts returnsReason:(NSString *)returnsReason pics:(NSString *)pics
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.failure = YES;
    result.success = NO;
    
    NSDictionary * params = [[NSDictionary alloc] init];
    
    if (pics.length > 0) {//有图片
        params = @{@"sn":sn,
                   @"returnsfor":returnsfor,
                   @"productIDs":productIDs,
                   @"contacts":contacts,
                   @"returnsReason":returnsReason,
                   @"pics":pics};
    } else {//没有图片
        params = @{@"sn":sn,
                   @"returnsfor":returnsfor,
                   @"productIDs":productIDs,
                   @"contacts":contacts,
                   @"returnsReason":returnsReason};
    }

    [self.http post:self.uniteUrl.sbpd_returnGoodsTextUrl params:params result:^(JKResponse *resp) {

        DLog(@"%@ %@",sn,productIDs);
        DLog(@"%@ message = %@",resp.content,resp.message);
        if (resp.OK) {
            result.success = YES;
            result.failure = NO;
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
    }];
}

@end







@interface SNReturnGoodsUpLoadPicDataManager ()
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@property (nonatomic, retain) JKHttpClient * http;
@end

@implementation SNReturnGoodsUpLoadPicDataManager

+ (SNReturnGoodsUpLoadPicDataManager *)shareManager
{
    AS_SINGLETON(SNReturnGoodsUpLoadPicDataManager, manager, {
        manager = [[SNReturnGoodsUpLoadPicDataManager alloc] init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.http = [[JKHttpClient alloc] init];
    })
    return manager;
}

//上传图片
- (void)returnGoodsPicWithResult:(SNReturnGoodsUploadImgHttpResult *)result params:(NSDictionary *)params imageData:(NSData *)image
{
    //    if ([self.http isRunning]) {
    //        return;
    //    }
    
    result.failure = YES;
    result.success = NO;
    
    [self.http upload:self.uniteUrl.sbpm_returnGoodsPicUrl data:image params:params result:^(JKResponse *resp) {
        DLog(@"%@",resp.request.URL);
        if (resp.OK) {
            result.success = YES;
            result.failure = NO;
            
            NSString * imageStr = resp.content[@"image"];
            int height = [resp.content[@"height"] intValue];
            int width = [resp.content[@"width"] intValue];
            
            NSString * uploadString = [NSString stringWithFormat:@"%@,%d,%d",imageStr,height,width];
            DLog(@"%@",resp.content[@"uploadString"]);
            [result.picsArray addObject:uploadString];
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
    }];
}

@end







//多次申请售后

@interface SNReturnGoodsRepetitiousDataManager ()
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@property (nonatomic, retain) JKHttpClient * http;
@end

@implementation SNReturnGoodsRepetitiousDataManager

+ (SNReturnGoodsRepetitiousDataManager *)shareManager
{
    AS_SINGLETON(SNReturnGoodsRepetitiousDataManager, manager, {
        manager = [[SNReturnGoodsRepetitiousDataManager alloc] init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.http = [[JKHttpClient alloc] init];
    })
    return manager;
}

- (void)returngoodsLoadDataWithResult:(SNReturnGoodsRepetitiousHttpResult *)result sn:(NSString *)sn productId:(NSString *)productId
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.failure = YES;
    result.success = NO;
    NSDictionary * parms = @{@"sn":sn,
                             @"product_id":productId};
    
    [self.http get:self.uniteUrl.sbpd_returnGoodsRepetitiousUrl params:parms result:^(JKResponse *resp) {
        DLog(@"%@",resp.request.URL);
        if (resp.OK) {
            DLog(@"%@",resp.content);
            result.success = YES;
            result.failure = NO;
            result.model = [SNReturnGoodsRepetitiousModel modelWithDict:resp.content];
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
        
    }];
}

@end
